﻿#!/usr/bin/env python3
# _*_ coding:utf-8 _*_
'''
@File    :   refresh_data.py
@Time    :   2022/02/02 15:06:12
@Author  :   chou 
@Version :   1.0
@Contact :   chou2079986882@gmail.com
@Desc    :   None
'''


# 定时删除fall数据设置在中午12:00
# 数据没10天删除一次 
import pymysql
from datetime import datetime 

def delete_fall_data():
    # 1.连接
    conn = pymysql.connect(host='localhost', user='daka', password='1234c', db='daka')
    # print(conn)
    if conn:
        print("连接成功")
    else:
        print('连接失败')
    # 2.创建游标
    cursor = conn.cursor()
    try:
        # 执行SQL语句
        sql = f"delete from fall"
        # print(sql)
        cursor.execute(sql)
        # 提交到数据库执行
        conn.commit()
    except:
        # 发生错误时回滚
        conn.rollback()
    cursor.close()
    print("执行完成")
    
def delete_all():
    # 1.连接
    conn = pymysql.connect(host='localhost', user='daka', password='1234c', db='daka')
    # print(conn)
    if conn:
        print("连接成功")
    else:
        print('连接失败')
    # 2.创建游标
    cursor = conn.cursor()
    try:
        # 执行SQL语句
        sql = f"delete from user WHERE stu_name not in('周纪元','向守琴','何炳松','邹景棋','高东','胡玉洁','王浩于','方天宇')"
        # print(sql)
        cursor.execute(sql)
        print("数据库初始化成功")
        # 提交到数据库执行
        conn.commit()
    except:
        # 发生错误时回滚
        conn.rollback()
    cursor.close()
    print("执行完成")


delete_fall_data()

# delete_all_data()
date=datetime.now().strftime("%d")
if date=='15':
    delete_all()
